package com.ankicoo.file.controller;

import com.aliyun.oss.OSS;
import com.ankicoo.file.config.OssConnect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import java.io.IOException;
import java.io.InputStream;

/**
 * Description: 文件处理
 *
 * @author : QCYANG
 * @date : 2021/1/19 10:15
 */
@RestController
@RequestMapping("/file")
public class FileController {
    @Autowired
    private OssConnect ossConnect;

    @PostMapping("/upload")
    public void uploadFile(StandardMultipartHttpServletRequest request){
        if (request.getFileMap().isEmpty()) {
            System.out.println("none");
        }else{
            for (MultipartFile file : request.getFileMap().values()) {
                String filename = file.getOriginalFilename();
                OSS oss = ossConnect.getOssClient();
                InputStream inputStream = null;
                try {
                    inputStream = file.getInputStream();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                oss.putObject(ossConnect.bucketName,filename, inputStream);
                oss.shutdown();
            }
        }
    }
}
